//
//  FQDChangePassViewController.m
//  FakeQDaily
//
//  Created by mewxiv on 9/11/16.
//  Copyright © 2016 Xensius. All rights reserved.
//

#import "FQDChangePassViewController.h"
#import "FQDNavigationView.h"
#import "FQDInputSettingCell.h"
#import "MBProgressHUD+FQDExtension.h"
#import "FQDAccountManager.h"

@interface FQDChangePassViewController () <UITableViewDataSource,
                                           UITableViewDelegate,
                                           UITextFieldDelegate>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSArray *sectionTitles;
@property (nonatomic, strong) NSMapTable *visibleCells;

@end

@implementation FQDChangePassViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.navView.titleLabel.text = @"修改密码";
    [self.navView.rightButton setTitle:@"保存" forState:UIControlStateNormal];
    [self.navView.rightButton addTarget:self action:@selector(savePassword:) forControlEvents:UIControlEventTouchUpInside];
    
    self.sectionTitles = @[ @"输入原始密码", @"输入新密码"];
    
    self.visibleCells = [NSMapTable mapTableWithKeyOptions:NSMapTableStrongMemory
                                          valueOptions:NSMapTableWeakMemory];
    
    CGSize viewSize = self.view.bounds.size;
    CGSize navSize = self.navView.frame.size;
    CGRect tableViewRect = CGRectMake(0, navSize.height, viewSize.width, viewSize.height - navSize.height);
    self.tableView = [[UITableView alloc] initWithFrame:tableViewRect style:UITableViewStyleGrouped];
    self.tableView.backgroundColor = [UIColor clearColor];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLineEtched;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
    
    [self.tableView registerClass:[FQDInputSettingCell class]
           forCellReuseIdentifier:[FQDInputSettingCell reuseId]];
}

#pragma mark - Event response
- (void)savePassword:(id)sender
{
    FQDInputSettingCell *cell1 = [self.visibleCells objectForKey:[NSIndexPath indexPathForRow:0 inSection:0]];
    FQDInputSettingCell *cell2 = [self.visibleCells objectForKey:[NSIndexPath indexPathForRow:0 inSection:1]];
    NSString *currentPassword = cell1.textField.text;
    NSString *desiredPassword = cell2.textField.text;
    NSString *trimmedPass1 = [currentPassword stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    NSString *trimmedPass2 = [desiredPassword stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    if ([trimmedPass1 length] <= 0 || [trimmedPass2 length] <= 0) {
        [MBProgressHUD showMessage:@"密码不能为空"];
        return;
    }

    if ([currentPassword length] < 8 || [desiredPassword length] < 8) {
        [MBProgressHUD showMessage:@"密码不能少于8位"];
        return;
    }

    [[QDailyAPI sharedAPI] changePassword:currentPassword newPassword:desiredPassword
     completion:^(id responseObject, NSError *error) {
        NSNumber *status = [responseObject valueForKeyPath:@"meta.status"];
        if ([status integerValue] == 200) {
            [self.navigationController popViewControllerAnimated:YES];
        } else {
            [MBProgressHUD showMessage:@"更新密码失败"];
        }
    }];
}

#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    FQDInputSettingCell *cell = (FQDInputSettingCell *)
    [tableView dequeueReusableCellWithIdentifier:[FQDInputSettingCell reuseId] forIndexPath:indexPath];
    cell.textField.secureTextEntry = YES;
    cell.textField.delegate = self;
    cell.textField.tag = indexPath.section;
    
    [self.visibleCells setObject:cell forKey:indexPath];
    
    return cell;
}

#pragma mark - UITableViewDelegate
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    CGRect labelFrame = CGRectMake(0, 0, tableView.frame.size.width, 40.0f);
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:labelFrame];
    titleLabel.font = FQDRegularFontWithSize(17.0f);
    titleLabel.textColor = RGB(128, 128, 128);
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.text = self.sectionTitles[section];
    return titleLabel;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 40.0f;
}

@end
